<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 后台管理
      </el-breadcrumb-item>

      <el-breadcrumb-item>类别列表</el-breadcrumb-item>
      <el-breadcrumb-item v-for="item in history">
        <span v-text="item.name"></span>
      </el-breadcrumb-item>

    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="100" header-align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键词列表" header-align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
      <el-table-column label="是否启用" width="100" align="center">
        <template slot-scope="scope">
  <!--        当前这一行绑定的整个的数据，自定义了以后就不要enable的prop了➡删掉-->
          <el-switch
              v-model="scope.row.enable"
              @change="toggleEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column  label="显示在导航栏" width="120" align="center">
        <template slot-scope="scope">
          <!--        当前这一行绑定的整个的数据，自定义了以后就不要enable的prop了➡删掉-->
          <el-switch
              v-model="scope.row.isDisplay"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </template>
      </el-table-column>
<!--      此处之所以叫isParent，是因为后端传给前端的对象，也就是CategoryListItemVO
          中的这个属性就是这么叫的，这一行就包括了这个属性-->
      <el-table-column  label="子级类别" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" :disabled="scope.row.isParent == 0"
                     @click="showSubCategoryList(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                      @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     :disabled="scope.row.isParent == 1"
                      @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px;" v-if="this.history.length > 0">
      <el-button style="float: right;" @click="goBack()">返回</el-button>
    </div>

    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="相册名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="相册简介" :label-width="formLabelWidth">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="相册排序序号" :label-width="formLabelWidth">
        <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {enable} from "core-js/internals/internal-metadata";

export default {
  data() {
    return {
      history:[],
      currentParentId: 0,
      tableData: [],
      /*点删除➡置为true（就能看到弹窗）*/
      dialogFormVisible: false,
      editForm: {
        id:'',
        name: '',
        description:'',
        sort:'',

      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    toggleEnable(category){
      let enableText =['禁用','启用'];
      let url = 'http://localhost:9080/categories/9527';
      if(category.enable == 1){
        url +='enable';
      }else {
        url +='disable'
      }
      console.log('url ='+url);
      this.axios.post(url).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          let message = '操作成功，已经将【'+ category.name+
              '】的状态改为【'+enableText[category.enable]+'】!';

          this.$message({
            message: message,
            type: 'success'
          });
        }else {
          this.$message.error(responseBody.message);
          this.loadCategoryList();
        }
      })
    },
    goBack(){
      // 减一就是最大下标，也就是点查看时记录的那一级的对象，也就是现在的父亲
      let parentCategory = this.history[this.history.length - 1];
      this.history.pop();
      // 返回上个页面跟这个父亲无关，和它的父级id有关➡把它的父级id编进请求的url
      this.currentParentId = parentCategory.parentId;
      this.loadCategoryList();
    },
    showSubCategoryList(category){
      // 使数组中元素的位置与分类属性中的深度联系起来
      this.history[category.depth - 1]=category;
      this.currentParentId=category.id;
      this.loadCategoryList();
    },
    submitEditForm(){
      let url = 'http://localhost:9080/albums/'+this.editForm.id+'/update';
      let formData = this.qs.stringify(this.editForm);
      this.axios.post(url,formData).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state ==20000){
          this.dialogFormVisible = false;
          this.$message({
            message: '修改相册信息成功',
            type: 'success'
          });
          this.loadCategoryList();
        }else if(responseBody.state ==40400){
          this.$alert(responseBody.message,'警告',{
            confirmButtonText:'确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadCategoryList();
            }
          });

        }else{
          this.$message.error(responseBody.message);
        }

      })
    },
    openEditDialog(category){
      let url = 'http://localhost:9080/albums/'+album.id;
      console.log('url = '+url);
      this.axios.get(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state ==20000){
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        }else{
          this.$alert(responseBody.message,'警告',{
            confirmButtonText:'确定',
            callback: action => {
              this.loadCategoryList();
            }
          })
        }
      })
    },
    openDeleteConfirm(category){
      let message = '此操作将永久删除【 '+ category.name +'】类别, 是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);
      }).catch(() => {

      });
    },
    handleDelete(category){
      let url = 'http://localhost:9080/categories/'+ category.id +'/delete';
      console.log('url =' + url)
      this.axios.post(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state == 20000||responseBody.state == 404000) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadCategoryList();
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },
    loadCategoryList(){
      let url = 'http://localhost:9080/categories/list-by-parent?parentId='+ this.currentParentId;
      console.log('url = ' + url);
      this.axios.get(url).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state ==20000){
          this.tableData = responseBody.data;
        }
      });
    }
  },
  mounted() {

    this.loadCategoryList()
  }
}
</script>

